package com.springboot.nacos.config;

import com.small.nacos.api.common.ApiCommonConstants;
import com.small.nacos.api.naming.common.NamingCommonConstants;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.Objects;
import java.util.Properties;


@ConfigurationProperties("small.nacos.config")
public class NacosConfigProperties {

    private String serverAddr;

    private String namespace;

    private String group = "DEFAULT_GROUP";

    private String name;

    private String fileExtension = "properties";

    private int timeout = 3000;

    /**
     * 是否开启配置中心
     */
    private Boolean enabled;

    /**
     * 长轮询时间
     */
    private String configLongPollTimeout;

    /**
     * 重试时间
     */
    private String configRetryTime;

    /**
     * 编码方式
     */
    private String encode;

    /**
     * 最大重试次数
     */
    private String maxRetry;


    private boolean refreshEnabled = true;

    public String getServerAddr() {
        return serverAddr;
    }

    public void setServerAddr(String serverAddr) {
        this.serverAddr = serverAddr;
    }

    public String getNamespace() {
        return namespace;
    }

    public void setNamespace(String namespace) {
        this.namespace = namespace;
    }

    public String getGroup() {
        return group;
    }

    public void setGroup(String group) {
        this.group = group;
    }

    public String getFileExtension() {
        return fileExtension;
    }

    public void setFileExtension(String fileExtension) {
        this.fileExtension = fileExtension;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Boolean getEnabled() {
        return enabled;
    }

    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }

    public String getConfigLongPollTimeout() {
        return configLongPollTimeout;
    }

    public void setConfigLongPollTimeout(String configLongPollTimeout) {
        this.configLongPollTimeout = configLongPollTimeout;
    }

    public String getConfigRetryTime() {
        return configRetryTime;
    }

    public void setConfigRetryTime(String configRetryTime) {
        this.configRetryTime = configRetryTime;
    }

    public void setRefreshEnabled(boolean refreshEnabled) {
        this.refreshEnabled = refreshEnabled;
    }

    public boolean isRefreshEnabled() {
        return this.refreshEnabled;
    }

    public String getEncode() {
        return encode;
    }

    public void setEncode(String encode) {
        this.encode = encode;
    }

    public String getMaxRetry() {
        return maxRetry;
    }

    public void setMaxRetry(String maxRetry) {
        this.maxRetry = maxRetry;
    }

    public int getTimeout() {
        return timeout;
    }

    public void setTimeout(int timeout) {
        this.timeout = timeout;
    }

    public Properties assembleConfigServiceProperties() {
        Properties properties = new Properties();
        properties.put(NamingCommonConstants.NAMESPACE, Objects.toString(this.namespace, ""));
        properties.put(NamingCommonConstants.SERVER_ADDR, Objects.toString(this.serverAddr, ""));
        properties.put(ApiCommonConstants.LONG_PULLING_TIMEOUT, Objects.toString(this.configLongPollTimeout, ""));
        properties.put(ApiCommonConstants.RETRY_TIME, Objects.toString(this.configRetryTime, ""));
        properties.put(ApiCommonConstants.ENCODE, Objects.toString(this.encode, ""));
        properties.put(ApiCommonConstants.MAX_RETRY, Objects.toString(this.maxRetry, ""));
        return properties;
    }

    @Override
    public String toString() {
        return "NacosConfigProperties{" +
                "serverAddr='" + serverAddr + '\'' +
                ", namespace='" + namespace + '\'' +
                ", group='" + group + '\'' +
                ", name='" + name + '\'' +
                ", fileExtension='" + fileExtension + '\'' +
                ", timeout=" + timeout +
                ", enabled=" + enabled +
                ", configLongPollTimeout='" + configLongPollTimeout + '\'' +
                ", configRetryTime='" + configRetryTime + '\'' +
                ", encode='" + encode + '\'' +
                ", maxRetry='" + maxRetry + '\'' +
                ", refreshEnabled=" + refreshEnabled +
                '}';
    }
}
